/*
 *  WaveTableWithWaveFunction.h
 *  SoundController0002
 *
 *  Created by jiro on 10/11/11.
 *  Copyright 2010 jiro music. All rights reserved.
 *
 */

#pragma once

#include "WaveTable.h"
#include "WaveFunction.h"

namespace sc {

	class WaveTableWithWaveFunction : public WaveTable
	{
	private:
	
		const WaveFunction& waveFunction;

	public:

		WaveTableWithWaveFunction( const WaveFunction& function, double adjuster )
			: WaveTable( WaveTable::Type_Function, function.GetCycle(), adjuster ),
			  waveFunction( function )
		{}
		
		bool operator == ( const WaveTable& rhs ) const
		{
			if ( this->WaveTable::operator == ( rhs ) ) {
				const WaveTableWithWaveFunction& rhsRef = dynamic_cast< const WaveTableWithWaveFunction& > ( rhs );
				return waveFunction == rhsRef.waveFunction;
			} 
			return false;
		}

		bool operator != ( const WaveTable& rhs ) const
		{
			return not( *this == rhs);
		}

		virtual bool operator < ( const WaveTable& rhs ) const 
		{
			if ( WaveTable::type != rhs.GetType() ) {
				return WaveTable::type < rhs.GetType();
			}
			if ( this->WaveTable::operator < ( rhs ) ) {
				return true;
			}
			const WaveTableWithWaveFunction& ref = dynamic_cast<const WaveTableWithWaveFunction& >( rhs );
			return waveFunction < ref.waveFunction;
		}

		double operator [] ( double time ) const
		{
			return adjuster * waveFunction( time );
		}
		
		const WaveFunction& GetWaveFunction() const 
		{
			return waveFunction;
		}
	};

}
